﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using FitAutomation.Server;
using MyUtils;

namespace FitAutomation
{
	static class Program
	{
		[STAThread]
		static void Main(string[] args)
		{
#if DEBUG
			Trace.AutoFlush = true;
#endif
			DebugUtils.DebuggerReattaching();

			if (args.Length > 0)
			{
				if (args.Any(x => "host".Equals(x, StringComparison.InvariantCultureIgnoreCase)))
				{
					// start intermediate server
					Trace.Listeners.Add(new TextWriterTraceListener(Path.Combine(Path.GetTempPath(), "FitAutomationHost.log")));
					try
					{
						var server = new FitAutomationHost();
						server.StartListening();
						ThreadPool.QueueUserWorkItem(delegate
							{
								try
								{
									server.Loop();
								}
								finally
								{
									System.Windows.Forms.Application.Exit();
								}
							});
						System.Windows.Forms.Application.Run();
					}
					catch (Exception ex)
					{
						Trace.TraceError(ex.ToString());
						throw;
					}
					return;
				}
			}

			// UI for fit automation browser (kind of client)
			var app = new System.Windows.Application();
			app.Run(new FitAutomation.WPF.MainWindow());
		}
	}
}
